package com.xiyou.luckdraw.web.controller;

import com.xiyou.luckdraw.api.dto.login.LoginUserInfoResponse;
import com.xiyou.luckdraw.api.dto.login.RolePermission;
import com.xiyou.luckdraw.api.dto.login.UserInfoDto;
import com.xiyou.luckdraw.api.dto.login.UserRoleInfo;
import com.xiyou.luckdraw.common.constants.ResultCodeConstants;
import com.xiyou.luckdraw.common.result.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: BaseController
 * @Description:
 * @author: limingxing
 * @Date: 2019-10-08 15:53
 */
public class BaseController {

    protected LoginUserInfoResponse getLoginUserInfo() {
        LoginUserInfoResponse user = (LoginUserInfoResponse) SecurityUtils.getSubject().getPrincipal();
        return user;
    }

    protected UserInfoDto getUserInfo() {
        return getLoginUserInfo().getUserInfo();
    }

    protected List<UserRoleInfo> getRoles() {
        return getLoginUserInfo().getRoleInfo();
    }

    protected List<RolePermission> getPermissions() {
        return getLoginUserInfo().getPermissionInfo();
    }

    protected String getUserName() {
        return getUserInfo().getNickName();
    }

    protected Long getUserId() {
        return getUserInfo().getId();
    }

    protected HttpServletRequest getHttpServletRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes())
                .getRequest();
        return request;
    }

    protected String getMessageId() {
        String messageId = getHttpServletRequest().getHeader("messageId");
        if (StringUtils.isBlank(messageId)) {
            messageId = UUID.randomUUID().toString();
        }
        return messageId;
    }



}
